﻿Public Class fZadaniVydaje
    'Dim cisloExistuje As Boolean = False
    Const NEVYPLNOVAT As String = "Nevyplňovat, pokud není uveden."
    Const VYPLNIT As String = "Je třeba vyplnit."
    Private Sub fZadaniVydaje_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Seznam_vstupyTableAdapter1.Fill(Me.DtbDataSet.seznam_vstupy)
        Me.Seznam_poznamkyTableAdapter1.Fill(Me.DtbDataSet.seznam_poznamky)
        Me.Seznam_PrijemPostovneTableAdapter1.Fill(Me.DtbDataSet.seznam_PrijemPostovne)
        Me.Seznam_PrijemTypPlatbyTableAdapter1.Fill(Me.DtbDataSet.seznam_PrijemTypPlatby)
        Me.Seznam_VydajFakturyTableAdapter1.Fill(Me.DtbDataSet.seznam_VydajFaktury)
        '----------------------------
        'fills chbox Fakturované výrobky
        Try
            For i As Long = 0 To Me.DtbDataSet.seznam_vstupy.Rows.Count() - 1
                Me.chboxFaktVyrobky.Items.Add(Me.DtbDataSet.seznam_vstupy.Rows.Item(i).Item(1))
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při získávání seznamu vstupů!")
            Me.btnZrusit.PerformClick()
        End Try
        'fills textboxes with constants
        Me.tboxNazevFirmy.Text = fZadaniVydaje.VYPLNIT
        Me.tboxJmeno.Text = fZadaniVydaje.NEVYPLNOVAT
        Me.tboxPrijmeni.Text = fZadaniVydaje.NEVYPLNOVAT
        Me.tboxAdresa.Text = fZadaniVydaje.VYPLNIT
        '-------------------------------------------
        '----------------gets last number of invoice and create new incrementally
        Try
            Dim pocRadkuFaktury As Long = Me.DtbDataSet.seznam_VydajFaktury.Rows.Count()
            'changes string lenght to 2, to ensure fixed lenght of var "datum"
            If _mesic.Length = 1 Then
                _mesic = "0" & _mesic
            Else
                _mesic = _mesic
            End If
            If _den.Length = 1 Then
                _den = "0" & _den
            Else
                _den = _den
            End If
            'get var "datum"
            Dim datum As String = _rok & "-" & _mesic & "-" & _den
            'get actual values of month and day again
            _mesic = Date.Today.Month
            _den = Date.Today.Day
            'get needed number of chars to substring from row of "seznam_VydajFaktury"
            Dim poslRadekFaktury As String = Me.DtbDataSet.seznam_VydajFaktury.Rows.Item(pocRadkuFaktury - 1).Item(1)
            Dim charsToConc As Long = vbNull
            Dim castCisla As String = vbNullString
            For i As Long = poslRadekFaktury.Length - 1 To 0 Step -1
                Dim ch As String = poslRadekFaktury.Substring(i, 1)
                If ch = "-" Then
                    Dim charsToConcStr As String = (poslRadekFaktury.Trim.Length - 1) - castCisla.Trim.Length
                    charsToConc = Convert.ToUInt64(charsToConcStr)
                    Exit For
                Else
                    castCisla = ch & castCisla
                End If
            Next i
            If pocRadkuFaktury = 0 Then 'if table is blank
                Me.tboxCisloFaktury.Text = datum & "-01"
                'or if string "datum" isn't the same as string from last row of the table "seznam_VydajFaktury"
            ElseIf datum <> Me.DtbDataSet.seznam_VydajFaktury.Rows.Item(pocRadkuFaktury - 1).Item(1).ToString _
                .Substring(0, charsToConc) Then
                Me.tboxCisloFaktury.Text = datum & "-01"
            Else 'the strings "datum" and last row of the table are the same, get "number" of the day and increment it by one
                Dim poslCislo As Long = vbNull
                Dim PoslCisloStr As String = ""
                Dim fill As String = ""
                For i As Long = poslRadekFaktury.Length - 1 To 0 Step -1
                    Dim ch As String = poslRadekFaktury.Substring(i, 1)
                    If ch = "-" Then
                        poslCislo = Convert.ToInt64(Trim(fill))
                        poslCislo += 1
                        Exit For
                    Else
                        fill = ch & fill 'This is IMPORTANT! Changing order of concat will yield wrong numbers, when working with 2+ digits numbers!!
                    End If
                Next i
                'convert var "poslCislo" as longInt to string and ensure proper lenght
                PoslCisloStr = Convert.ToString(poslCislo)
                If PoslCisloStr.Length = 1 Then
                    PoslCisloStr = "0" & PoslCisloStr
                Else
                    PoslCisloStr = PoslCisloStr
                End If
                Me.tboxCisloFaktury.Text = datum & "-" & PoslCisloStr
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při generování čísla faktury!")
            Me.btnZrusit.PerformClick()
        End Try
        '-------------------end
        'clears status label text 
        My.Forms.fMain.lblTStrip1.Text = ""
    End Sub

    Private Sub btnZrusit_Click(sender As System.Object, e As System.EventArgs) Handles btnZrusit.Click
        My.Forms.fMain.lblTStrip1.Text = ZRUSENO
        Me.Dispose()
        Me.Close()
    End Sub

    Private Sub btnPokracovat_Click(sender As System.Object, e As System.EventArgs) Handles btnPokracovat.Click
        Dim fill As String = ""
        'checking for missing values
        If Me.tboxCisloFaktury.Text.Length = 0 Then
            fill = fill & Me.lblCisloFaktury.Text & vbCrLf
            Me.tboxCisloFaktury.BackColor = Color.Red
        End If
        If Me.tboxAdresa.Text.Length = 0 Or Me.tboxAdresa.Text = fZadaniVydaje.VYPLNIT Then
            fill = fill & Me.lblAdresa.Text & vbCrLf
            Me.tboxAdresa.BackColor = Color.Red
        End If
        If Me.chboxFyzOsoba.Checked = True Then
            If Me.tboxJmeno.Text.Length = 0 Or Me.tboxJmeno.Text = fZadaniVydaje.VYPLNIT Then
                fill = fill & Me.lblJmeno.Text & vbCrLf
                Me.tboxJmeno.BackColor = Color.Red
            End If
            If Me.tboxPrijmeni.Text.Length = 0 Or Me.tboxPrijmeni.Text = fZadaniVydaje.VYPLNIT Then
                fill = fill & Me.lblPrijmeni.Text & vbCrLf
                Me.tboxPrijmeni.BackColor = Color.Red
            End If
        End If
        If Me.chboxFyzOsoba.Checked = False Then
            If Me.tboxNazevFirmy.Text.Length = 0 Or Me.tboxNazevFirmy.Text = fZadaniVydaje.VYPLNIT Then
                fill = fill & Me.lblNazevFirmy.Text & vbCrLf
                Me.tboxNazevFirmy.BackColor = Color.Red
            End If
        End If
        If Me.tboxPostovne.Text.Length = 0 Then
            Me.tboxPostovne.Text = vbNullString
        End If
        If Me.chboxFaktVyrobky.CheckedItems.Count() = 0 Then
            fill = fill & Me.lblFaktVyrobky.Text & vbCrLf
            Me.chboxFaktVyrobky.BackColor = Color.Red
        End If
        If fill.Length = 0 And Me.chboxFaktVyrobky.CheckedItems.Count() > 0 Then
            'clears notification text before saving data to dtb's tables
            If Me.tboxNazevFirmy.Text.Contains(fZadaniVydaje.NEVYPLNOVAT) Then
                Me.tboxNazevFirmy.Text = ""
            End If
            If Me.tboxJmeno.Text.Contains(fZadaniVydaje.NEVYPLNOVAT) Then
                Me.tboxJmeno.Text = ""
            End If
            If Me.tboxPrijmeni.Text.Contains(fZadaniVydaje.NEVYPLNOVAT) Then
                Me.tboxPrijmeni.Text = ""
            End If
            My.Forms.fZadaniVydajePolozkyDialog.ShowDialog()
        Else
            MsgBox("Je třeba vyplnit následující položky:" & vbCrLf & fill, MsgBoxStyle.Exclamation, "Chyba!")
        End If
    End Sub


    Private Sub tboxAdresa_Click(sender As Object, e As System.EventArgs) Handles tboxAdresa.Click
        If Me.tboxAdresa.Text.Length > 0 Then
            Me.tboxAdresa.SelectAll()
        End If
    End Sub

    Private Sub tboxAdresa_LostFocus(sender As Object, e As System.EventArgs) Handles tboxAdresa.LostFocus
        If Me.tboxAdresa.Text.Length > 0 And Me.tboxAdresa.BackColor = Color.Red Then
            Me.tboxAdresa.BackColor = Color.White
        End If

    End Sub

    Private Sub tboxJmeno_Click(sender As Object, e As System.EventArgs) Handles tboxJmeno.Click
        If Me.tboxJmeno.Text.Length > 0 Then
            Me.tboxJmeno.SelectAll()
        End If
    End Sub

    Private Sub tboxJmeno_LostFocus(sender As Object, e As System.EventArgs) Handles tboxJmeno.LostFocus
        If Me.chboxFyzOsoba.Checked = True Then
            If Me.tboxJmeno.Text.Length > 0 And Me.tboxJmeno.BackColor = Color.Red Then
                Me.chboxFyzOsoba.BackColor = Color.White
            End If
        End If
    End Sub

    Private Sub tboxNazevFirmy_Click(sender As Object, e As System.EventArgs) Handles tboxNazevFirmy.Click
        If Me.tboxNazevFirmy.Text.Length > 0 Then
            Me.tboxNazevFirmy.SelectAll()

        End If
    End Sub

    Private Sub tboxNazevFirmy_LostFocus(sender As Object, e As System.EventArgs) Handles tboxNazevFirmy.LostFocus
        If Me.chboxFyzOsoba.Checked = False Then
            If Me.tboxNazevFirmy.Text.Length > 0 And Me.tboxNazevFirmy.BackColor = Color.Red Then
                Me.tboxNazevFirmy.BackColor = Color.White
            End If
        End If
    End Sub


    Private Sub tboxPrijmeni_Click(sender As Object, e As System.EventArgs) Handles tboxPrijmeni.Click
        If Me.tboxPrijmeni.Text.Length > 0 Then
            Me.tboxPrijmeni.SelectAll()
        End If
    End Sub

    Private Sub tboxPrijmeni_LostFocus(sender As Object, e As System.EventArgs) Handles tboxPrijmeni.LostFocus
        If Me.chboxFyzOsoba.Checked = True Then
            If Me.tboxPrijmeni.Text.Length > 0 And Me.tboxPrijmeni.BackColor = Color.Red Then
                Me.tboxPrijmeni.BackColor = Color.White
            End If
        End If
    End Sub

    Private Sub chboxFaktVyrobky_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles chboxFaktVyrobky.SelectedIndexChanged
        If Me.chboxFaktVyrobky.CheckedItems.Count() > 0 And Me.chboxFaktVyrobky.BackColor = Color.Red Then
            Me.chboxFaktVyrobky.BackColor = Color.White
        End If
    End Sub

    Private Sub chboxFyzOsoba_CheckStateChanged(sender As Object, e As System.EventArgs) Handles chboxFyzOsoba.CheckStateChanged
        If Me.chboxFyzOsoba.CheckState = CheckState.Checked Then
            Me.tboxNazevFirmy.Text = fZadaniVydaje.NEVYPLNOVAT
            Me.tboxJmeno.Text = fZadaniVydaje.VYPLNIT
            Me.tboxAdresa.Text = fZadaniVydaje.VYPLNIT
            Me.tboxPrijmeni.Text = fZadaniVydaje.VYPLNIT
        ElseIf Me.chboxFyzOsoba.CheckState = CheckState.Unchecked Then
            Me.tboxNazevFirmy.Text = fZadaniVydaje.VYPLNIT
            Me.tboxJmeno.Text = fZadaniVydaje.NEVYPLNOVAT
            Me.tboxAdresa.Text = fZadaniVydaje.VYPLNIT
            Me.tboxPrijmeni.Text = fZadaniVydaje.NEVYPLNOVAT
        End If
    End Sub

    Private Sub tboxPostovne_LostFocus(sender As Object, e As System.EventArgs) Handles tboxPostovne.LostFocus
        If Me.tboxPostovne.Text.Length > 0 And Me.tboxPostovne.Text.Contains(",") = True Then
            Me.tboxPostovne.Text = Me.tboxPostovne.Text.Replace(",", ".")
        End If
    End Sub

    Private Sub btnPridatVstup_Click(sender As System.Object, e As System.EventArgs) Handles btnPridatVstup.Click
        Me.btnPridatVstup.DialogResult = Windows.Forms.DialogResult.Yes
        My.Forms.fSetVstupyDialog.ShowDialog()
    End Sub

    Private Sub tboxPostovne_TextChanged(sender As Object, e As System.EventArgs) Handles tboxPostovne.TextChanged
        Dim post As TextBox = Me.tboxPostovne
        If post.Text.Length > 0 Then
            If Decimal.TryParse(post.Text.Trim, Decimal.One) = False Then
                If post.Text.Contains(",") = True Then
                    post.Text = post.Text.Replace(",", ".")
                    post.SelectionStart = post.Text.Length
                    post.Select()
                Else
                    MsgBox("Desetinné číslo není správně zadáno!", MsgBoxStyle.Critical, "Chyba")
                    Exit Sub
                End If
            End If
        End If
    End Sub
End Class